package com.testfileupload.uploadimg.design.combination.optimization;


import java.util.ArrayList;

public class Client {
    public static void main(String[] args) {
        //组装组织结构
        Branch ceo = compositeCorpTree();
        //打印CEO
        System.out.println(ceo.getInfo());
        //所有员工的信息
        System.out.println(getTreeInfo(ceo));
    }

    private static String getTreeInfo(Branch ceo) {
        ArrayList<ICorp> subordinate = ceo.getSubordinate();
        String info = "";
        for (ICorp s : subordinate) {
            if (s instanceof Leaf) {
                info = info + s.getInfo()+ "\n";
            } else {
                info = info + s.getInfo() + "\n" + getTreeInfo((Branch) s);
            }
        }
        return info;
    }

    private static Branch compositeCorpTree() {
        //产生根节点
        Branch ceo = new Branch("王大麻子", "总经理", 100000);
        //三个部门经理
        Branch developDep = new Branch("刘da", "研发部门经理", 10000);
        Branch salesDep = new Branch("马二", "销售经理", 20000);
        Branch finaceDep = new Branch("赵三", "财务经理", 30000);
        //开发小组长
        Branch firstBranchGroup = new Branch("杨三", "开发一组组长", 5000);
        Branch secondBranchGroup = new Branch("武大", "开发二组组长", 6000);

        //小兵
        ILeaf a = new Leaf("a", "开发", 3999);
        ILeaf b = new Leaf("b", "开发", 3999);
        ILeaf c = new Leaf("c", "开发", 3999);
        ILeaf d = new Leaf("d", "销售", 2999);
        ILeaf e = new Leaf("e", "销售", 2000);
        ILeaf f = new Leaf("f", "财务", 1999);
        ILeaf k = new Leaf("k", "秘书", 7999);
        ILeaf g = new Leaf("g", "研发副总", 9999);

        ceo.addSubordinate(developDep);
        ceo.addSubordinate(salesDep);
        ceo.addSubordinate(finaceDep);
        ceo.addSubordinate(k);
        developDep.addSubordinate(firstBranchGroup);
        developDep.addSubordinate(secondBranchGroup);
        developDep.addSubordinate(g);
        firstBranchGroup.addSubordinate(a);
        firstBranchGroup.addSubordinate(b);
        secondBranchGroup.addSubordinate(c);

        salesDep.addSubordinate(d);
        salesDep.addSubordinate(e);

        finaceDep.addSubordinate(f);
        return ceo;
    }
}
